package com.sinosoft.dsp.platform.action;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.event.ActionEvent;

import com.sinosoft.dsp.platform.action.helper.ActionHelper;
import com.sinosoft.dsp.platform.components.portal.input.ISinoSearchFormViewBean;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.platform.manager.ApplicationController;
import com.sinosoft.dsp.platform.utils.FacesContextUtils;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.utils.Const;
import com.sinosoft.dsp.system.utils.ReflectionUtils;

@ManagedBean(name = "searchFormAction")
@RequestScoped
public class SearchFormAction {

	public static final String SEARCH_FORM_PARAMETER_ACTION_NAME = "actionName";

	public static final String SEARCH_FORM_PARAMETER_METHOD_NAME = "methodName";

	public static final String SEARCH_FORM_PARAMETER_VIEWBEAN_ID = "viewBeanId";

	public static final String SEARCH_FORM_PARAMETER_SEARCH_INDEX = "searchIndex";

	public static final String SEARCH_FORM_PARAMETER_TAG_ELEMENT_ID = "tagElementId";

	private static final Log log = Log.getLog(SearchFormAction.class.getName());

	@ManagedProperty(value = "#{applicationController}")
	private ApplicationController applicationController;

	public void buttonAction(ActionEvent actionEvent) {
		try {
			UIComponent btn = actionEvent.getComponent();
			String actionClassName = (String) ActionHelper
					.getActionEventParameterValue(btn,
							SEARCH_FORM_PARAMETER_ACTION_NAME);
			String methodName = (String) ActionHelper
					.getActionEventParameterValue(btn,
							SEARCH_FORM_PARAMETER_METHOD_NAME);

			if (!Const.isEmpty(actionClassName) && !Const.isEmpty(methodName)) {
				Object action = SinoManagedBean.findBean(actionClassName);
				ReflectionUtils.invokeMethod(action, methodName, actionEvent);
			}

		} catch (Exception e) {
			log.error("查询表单的按钮提交事件执行出现错误。");
			FacesContextUtils.addGlobalErrorFacesMessage("查询表单的按钮提交事件执行出现错误。");
		}

	}

	public void submitAction(ActionEvent actionEvent) {
		try {
			UIComponent btn = actionEvent.getComponent();
			String viewBeanId = (String) ActionHelper
					.getActionEventParameterValue(btn,
							SEARCH_FORM_PARAMETER_VIEWBEAN_ID);
			String searchIndex = (String) ActionHelper
					.getActionEventParameterValue(btn,
							SEARCH_FORM_PARAMETER_SEARCH_INDEX);
			if (Const.isEmpty(searchIndex)) {
				searchIndex = "0";
			}

			int index = Integer.valueOf(searchIndex);

//			ISinoSearchFormViewBean form = (ISinoSearchFormViewBean) applicationController
//					.getViewBeanById(viewBeanId);
//			form.getSearchs()[index].getDatas().reload();

		} catch (Exception e) {
			log.error("提交表单查询事件执行出现错误。");
			FacesContextUtils.addGlobalErrorFacesMessage("提交表单查询事件执行出现错误。");
		}
	}

	public ApplicationController getApplicationController() {
		return applicationController;
	}

	public void setApplicationController(
			ApplicationController applicationController) {
		this.applicationController = applicationController;
	}

}
